using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HeathBarUI : MonoBehaviour
{
    private Entity entity;
    private StatCharacter stat;
    private float nowHP;
    private float maxHp;
    [SerializeField] private Slider hpSlider;
    private void Start()
    {
        entity = GetComponentInParent<Entity>();
        stat = GetComponentInParent<StatCharacter>();
        entity.FlipEvent += NoFlip;
        stat.OnStatHPChangeEvent += UpdateHealthUI;
        Invoke("UpdateHealthUI", .2f);
    }
    private void UpdateHealthUI()
    {
        maxHp = entity.stat.GetMaxHpValue();
        nowHP = entity.stat.currentHp;
        hpSlider.value = nowHP / maxHp;
    }
    private void NoFlip()
    {
        transform.Rotate(0, -180, 0);
    }
    private void OnDestroy()
    {
        entity.FlipEvent -= NoFlip;
        stat.OnStatHPChangeEvent -= UpdateHealthUI;
    }
}
